<template>
  <!-- 无畏契约自动获取授权 -->
  <el-dialog
    :title="'扫码授权'"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="300px"
    append-to-body
    @close="close"
  >
    <div class="form-box" v-loading="loading">
      <div class="demo-ruleForm">
        <img :src="qrCode" alt="" style="width: 200px; height: 200px" />
        <div class="center">
          {{
            msg == "二维码认证中" ? "已扫码，等待授权" : "请扫描二维码进行授权"
          }}
        </div>
        <div><el-button @click="checkQrcode" type="primary" size="mini">我已授权</el-button></div>
      </div>
    </div>
  </el-dialog>
</template>
  
  <script>
import autoApi from "@/api/autoProduction/information.js"
export default {
  name: "addFina",
  props:["fromType"],
  data() {
    return {
      dialogVisible: false,
      checkPending: false,
      loading: false,
      msg: '',
      qrCode: '',
      uuid: '',
      timeOutTimer: null
    };
  },
  created() {

  },
  beforeDestroy() {
    this.closeTimer();
  },
  methods: {
    // 打开弹窗
    open() {
      this.dialogVisible = true
      this.loading = false
      this.getQrCode()
    },
    close() {
      this.closeTimer();
    },
    getQrCode() {
      this.loading = true
      this.checkPending = false;
      this.closeTimer();
      if(this.fromType=='attrs'){
        autoApi.huoqugetWWQRcode().then(res => {
          this.loading = false
          if (res.code == 200) {
            this.qrCode = res.data.base64Img
            this.uuid = res.data.uuid
            // this.getTokenInterval()
          }
        }).catch(err=>{
          this.loading = false
        })
      }else{
        autoApi.getWWQRcode().then(res => {
          this.loading = false
          if (res.code == 200&&res.data.code==200) {
            this.qrCode = res.data.data.base64Img
            // console.log("%c Line:74 🌽 this.qrCode", "color:#b03734", this.qrCode);
            this.uuid = res.data.data.uuid
            if (!this.checkPending) this.checkQrcode();
          }
        }).catch(err=>{
          this.loading = false
        })
      }
      
    },
    // 关闭延时器
    closeTimer() {
      if (this.timeOutTimer) clearTimeout(this.timeOutTimer);
      this.timeOutTimer = null;
    },
     // 校验二维码状态
     async checkQrcode() {
      let res;
      this.checkPending = true;
      try {
          res = await autoApi.getWWcheckCode({
            uuid: this.uuid,
          });
        } catch (e) {
          if (this.dialogVisible) {
            this.getQrCode();
          }
        }
      this.checkPending = false;
      this.closeTimer();
      if (res && res.code==200) {
        if (
          this.dialogVisible &&
          (res.data.msg == '二维码未失效'||res.data.msg == '二维码认证中')
        ) {
          // 延时1秒后查询二维码状态
          this.timeOutTimer = setTimeout(() => {
            if (this.dialogVisible) {
              this.checkQrcode();
            }
          }, 2000);
        } else if (
          this.dialogVisible &&res.data.msg == '登录成功'
        ) {
          // 已授权  开始获取数据
          // this.loginBody = res.data.data.data
          this.getToken();
        } else if(this.dialogVisible){
          // 失效 重新获取
          this.getQrCode();
        }
      }
    },
    getToken() {
      let obj = {
        uuid: this.uuid
      }
      this.loading = true
      if(this.fromType=='attrs'){
        autoApi.huoqupostWWToken(obj).then(res => {
          this.msg = res.msg
          this.loading = false
          if (res.code==200) {
            this.$emit('success')
            // window.clearInterval(this.timer)
            // this.timer = null
            this.$alert('同步成功！')
            this.dialogVisible = false

          }
        }).catch(err => {
          this.loading = false
          if (err.code == 500) {
            this.getQrCode()
          }
        })
      }else{
        autoApi.postWWToken(obj).then(res => {
          this.loading = false
          if(res.code==200){
            this.msg = res.data.msg
            if (res.data.code==200) {
              this.$emit('success', res.data.data)
              // window.clearInterval(this.timer)
              // this.timer = null
              this.dialogVisible = false
            }else if(res.data.code==500){
              this.getQrCode()
            }
          }
          
        }).catch(err => {
          this.loading = false
          if (err.code == 500) {
            this.getQrCode()
          }
        })
      }
      
    },
    // getTokenInterval() {
    //   window.clearInterval(this.timer)
    //   this.timer = window.setInterval(() => {
    //     this.getToken()
    //   }, 5000)
    // }

  }
};
  </script>
  <style scoped lang="css">
.demo-ruleForm {
  text-align: center;
}
</style>
  